#include "wab_include.h"

void L_Render();

WAB_Viewport::WAB_Viewport()
{
    this->view_width  = 0;
    this->view_height = 0;

    this->view_xPos = 0;
    this->view_yPos = 0;

    this->initialized = false;
}

WAB_Viewport::WAB_Viewport(int32 width, int32 height, int32 xPos, int32 yPos)
{
    this->view_width = width;
    this->view_height = height;

    this->view_xPos = xPos;
    this->view_yPos = yPos;
    
    this->initialized = true;
}

void WAB_Viewport::loadViewport()
{
    if (!initialized)
        return;

    WAB_GL_CreateViewport(view_xPos, view_yPos, view_width, view_height);
}

void WAB_Viewport::setPosition(int32 xPos, int32 yPos)
{
    this->view_xPos = xPos;
    this->view_yPos = yPos;
}

void WAB_Viewport::setDimensions(int32 width, int32 height)
{
    this->view_width  = width;
    this->view_height = height;

    this->initialized = true;
}

void WAB_Viewport::getDimensions(int32* width, int32* height, int32* xPos, int32* yPos)
{
    if (!initialized)
        return;

    if (width)        *width  = view_width;
    if (height)       *height = view_height;
    if (xPos)         *xPos = view_xPos;
    if (yPos)         *yPos = view_yPos;
}

WAB_Screen::WAB_Screen(int32 width, int32 height, int32 winX, int32 winY)
{
    this->screen_width = width;
    this->screen_height = height;

    this->window_x = winX;
    this->window_y = winY;

    this->numViewports = 0;

    initialized = false;

    WAB_MemZero(screenTitle, kStrBufferLen_Medium);
    WAB_MemZero(viewports, sizeof(WAB_Viewport));
}

void WAB_Screen::init()
{
    if (initialized)
        return;

    WAB_GL_InitWindow(GLUT_DOUBLE | GLUT_RGBA, screen_width, screen_height, window_x, window_y);
    WAB_GL_CreateWindow(screenTitle);
    WAB_GL_InitOrtho2D(GL_PROJECTION, GL_MODELVIEW, screen_width * -1, screen_width, screen_height, screen_height * -1);

    setRenderFunction(L_Render);
}

void WAB_Screen::swapBuffers()
{
    WAB_GL_SwapBuffer();
}

void WAB_Screen::runMainLoop()
{
    WAB_GL_MainLoop();
}

WAB_Viewport* WAB_Screen::addViewport(int32 width, int32 height, int32 x, int32 y)
{
    if (numViewports >= NUM_MAX_VIEWPORTS)
        return NULL;

    WAB_Viewport* pViewport = NULL;
    pViewport = &viewports[this->numViewports++];
    if (pViewport)
    {
        pViewport->setDimensions(width, height);
        pViewport->setPosition(x, y);
    }

    return pViewport;
}

void WAB_Screen::setRenderFunction(glDisplayFunc render)
{
    WAB_GL_SetDisplayFunc(render);
}

void WAB_Screen::setTitle(const char* title)
{
    if (!title)
        return;

    WAB_StrCpy(screenTitle, &title, kStrBufferLen_Medium);

    if (initialized)
        WAB_GL_SetWindowTitle(screenTitle);
}

void WAB_Screen::getDimensions(int32* width, int32* height, int32* xPos, int32* yPos)
{
    if (width)    *width  = screen_width;
    if (height)   *height = screen_height;
    if (xPos)     *xPos = window_x;
    if (yPos)     *yPos = window_y;
}

extern WAB_Screen screen;

void L_Render()
{
    glClear(GL_COLOR_BUFFER_BIT);

    int radius = 300;
    int numPoints = 200;
    float theta = 2 * M_PI / float(numPoints);
    float cos = cosf(theta);
    float sin = sinf(theta);
    float angle;

    float x = float(radius);
    float y = 0;

    glBegin(GL_POLYGON);
    for (int i = 0; i < numPoints; ++i)
    {
        glColor3f(255, 0, 0);
        glVertex2f(x, y);
        angle = x;
        x = cos * x - sin * y;
        y = sin * angle + cos * y;
    }
    glEnd();
    glFlush();

    WAB_GL_SwapBuffer();
}
